package com.zlq.cn.service.lovers;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.config.Config;
import com.zlq.cn.entity.LoversBindUser;
import com.zlq.cn.entity.User;
import com.zlq.cn.mapper.lovers.LoversBindUserMapper;
import com.zlq.cn.service.un.PushService;
import com.zlq.cn.service.un.UserService;
import one.util.streamex.StreamEx;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2022-05-08
 */
@Service
public class LoversBindUserService extends ServiceImpl<LoversBindUserMapper, LoversBindUser> {

    @Resource
    private UserService userService;

    @Resource
    private PushService pushService;

    public void bind(String invitationCode) {
        User user = SignApiBootstrap.requestUser();
        User toUser = userService.lambdaQuery()
                .eq(User::getInvitationCode, invitationCode)
                .last(" LIMIT 1 ")
                .one();
        Assert.notNull(toUser, String.format("配对码[%s]不存在", invitationCode));
        Integer count = super.lambdaQuery()
                .eq(LoversBindUser::getLoversBindFromUserId, user.getId())
                .eq(LoversBindUser::getLoversBindToUserId, toUser.getId())
                .count();
        Assert.state(count == 0, String.format("配对码[%s]已绑定", invitationCode));
        super.save(new LoversBindUser()
                .setLoversBindFromUserId(user.getId())
                .setLoversBindToUserId(toUser.getId()));
        pushService.pushAccount(Config.PROJECT_LOVERS, toUser.getId(), "配对成功", "配对成功，快来看看吧！", null);
    }

    public void unAllBind() {
        User user = SignApiBootstrap.requestUser();
        super.lambdaUpdate()
                .eq(LoversBindUser::getLoversBindFromUserId, user.getId())
                .or()
                .eq(LoversBindUser::getLoversBindToUserId, user.getId())
                .remove();
    }

    public List<User> getBind() {
        Set<Integer> userIds = this.getBindUserIds();
        if (CollectionUtil.isNotEmpty(userIds)) {
            return userService.listByIds(userIds);
        }
        return Collections.emptyList();
    }

    public Set<Integer> getBindUserIds() {
        User user = SignApiBootstrap.requestUser();
        return super.lambdaQuery()
                .eq(LoversBindUser::getLoversBindFromUserId, user.getId())
                .or()
                .eq(LoversBindUser::getLoversBindToUserId, user.getId())
                .list().stream()
                .flatMap(it -> StreamEx.of(it.getLoversBindFromUserId(), it.getLoversBindToUserId()))
                .filter(it -> !user.getId().equals(it))
                .collect(Collectors.toSet());
    }
}
